package view
{
	import com.greensock.easing.Cubic;
	import com.greensock.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	import model.Image;
	import model.Shake;

	public class GameSelect extends Sprite
	{
		
	
		private var iconArr:Array=[];
		private var iconUrlArr:Array=[];
		//图标间距
		private const distance:uint = 10;
		private const iconHeight:uint = 120;
		
		public static const COMPLETE:String = "complete";
		public function GameSelect()
		{
			super();
			
			var str1:String = "assets/图标猪2.swf";
			var str2:String = "assets/找茬图标.png";
			var str3:String = "assets/宠物图标small.png";
			iconUrlArr.push(str1,str2,str3);
			loadIcon();			
		}
		
		
		//加载图标
		private function loadIcon():void
		{
			for(var i:uint=0; i<iconUrlArr.length; i++){
				var image:Image = new Image(iconUrlArr[i]);
				iconArr.push(image);
				this.addChild(image);
				image.addEventListener(Image.COMPLETE,onComplete);
				image.addEventListener(MouseEvent.MOUSE_OVER,onOver,false,0,true);
				
			}
		}
		
		
		//加载完成发广播
		private var counting:uint=0;
		protected function onComplete(event:Event):void
		{
			counting ++;
			event.currentTarget.removeEventListener(Image.COMPLETE,onComplete)
			if(counting == iconUrlArr.length){
				arrange();				
				dispatchEvent(new Event(Image.COMPLETE));
			}								
		}
		
		
		//排列位置
		private function arrange():void
		{
			for(var i:uint=0; i<iconArr.length; i++){
				
				iconArr[i].y = i * (iconHeight + distance);
			}
			appear();
		}

		private function appear():void
		{								
				x = -width;
				y = 95;
				TweenLite.to(this, 0.2, {x:30, ease:Cubic.easeOut});						
		}
		
		/**
		 *隐藏这个模块 ,并从显示列表删除。
		 * 
		 */		
		public function meHide():void
		{			
				TweenLite.to(this, 0.5, {x:-width, ease:Cubic.easeOut});	
				setTimeout(removeMe,600);
				
		}
		 
		private function removeMe():void
		{						
			if(parent){
				parent.removeChild(this);			
			}
		}
		
		/**
		 *实现悬浮抖动<></n>
		 * 鼠标悬浮抖动相应图标，并发出抖动事件。urlIcon是抖动图标的名字
		 *  
		 */		
		//抖动
		private var shakeSwitch:Boolean = true;
		//抖动时间
		private var shakeTime:Number = 0.1;
		//抖动幅度
		private var shakeRange:uint = 4;
		//抖动的图标name
		public var urlIcon:String;
		//抖动事件
		public static const  BOARD:String = "blackboard";
		protected function onOver(event:MouseEvent):void
		{
			if(shakeSwitch)
			{
				Shake.ShakeObject(event.currentTarget as DisplayObject,shakeRange,shakeTime);
				shakeSwitch = false;
			}			
			setTimeout(function():void{shakeSwitch = true;}, shakeTime*2000);			
			urlIcon = event.currentTarget.url;
			dispatchEvent(new Event(GameSelect.BOARD));
		}		 				
	
	}
}